<template>
  <MesBox
    title="新建"
    v-model="visible"
    :width="800"
    :height="300"
    :onModelClose="onModelClose"
  >
    <MesForm
      ref="myform"
      :formFields="formFields"
      :formRules="formRules"
    ></MesForm>
    <template #footer>
      <el-button type="danger" @click="save">
        <i class="el-icon-check"></i>
        保存</el-button
      >
    </template>
  </MesBox>
</template>

<script setup>
import MesBox from '@/components/basic/MesBox.vue'
import MesForm from '@/components/basic/MesForm.vue'
import { getCurrentInstance, ref } from 'vue'

const emit = defineEmits(['parentCall'])
const myform = ref(null)
const that = getCurrentInstance().appContext.config.globalProperties
let visible = ref(false)
let formFields = ref({
  OrderNum: '',
  ProductCode: '',
  Qty: 0,
})
const formRules = [
  [
    {
      title: '采购单号',
      field: 'OrderNum',
      type: 'select',
      dataKey: 'PurchaseOrderNum',
      required: true,
    },
  ],
  [
    {
      title: '产品编码',
      field: 'ProductCode',
      type: 'select',
      dataKey: 'productCode',
      min: 0,
      required: true,
    },
  ],
  [
    {
      title: '数量',
      field: 'Qty',
      type: 'number',
      min: 0,
      required: true,
    },
  ],
]
const open = () => {
  visible.value = true
}

const save = () => {
  myform.value.validate(async (valid) => {
    if (valid) {
      let res = await that.http.post(
        '/api/Production_WorkOrder/AddOutRawMaterial',
        formFields.value,
        true
      )
      if (res.status) {
        that.$message.success('新建成功')
        onModelClose()
        emit('parentCall', ($parent) => {
          visible.value = false
          $parent.search()
        })
      } else {
        that.$message.error(res.message)
      }
    }
  })
}

const onModelClose = () => {
  visible.value = false
  formFields.value.OrderNum = ''
  formFields.value.ProductCode = ''
  formFields.value.Qty = 0
}

defineExpose({ open })
</script>

<style lang="scss" scoped></style>
